﻿using Domain.Entitys.Base_Manage;
using Domain.IRepositorys.Base_Manage;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys.Base_Manage
{
    public class DictdataRepository : IDictdataRepository
    {
        private readonly MyDbContext myDbContext;

        public DictdataRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        /// <summary>
        /// 根据字典类型、状态、标签查询总条数
        /// </summary>
        /// <param name="Dict_type"></param>
        /// <param name="Dict_label"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Getsys_Dict_DatasCount(string Dict_type, string Dict_label, char Status)
        {
            //只查询没有被删除的数据
            IQueryable<Sys_Dict_Data> query = myDbContext.Sys_Dict_Data.Where(d => d.Del_flag == '0');
            //条件筛选
            if (!string.IsNullOrEmpty(Dict_type))
            {
                query = query.Where(d => d.Dict_type == Dict_type);
            }
            if (!string.IsNullOrEmpty(Dict_label))
            {
                query = query.Where(d => d.Dict_label == Dict_label);
            }
            if (Status == '0' || Status == '1')
            {
                query = query.Where(d => d.Status == Status);
            }
            return query.Count();
        }
        /// <summary>
        /// 分页查询、条件查询
        /// </summary>
        /// <param name="Dict_type"></param>
        /// <param name="Dict_label"></param>
        /// <param name="Status"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Sys_Dict_Data> Getsys_Dict_DatasWhere(string Dict_type, string Dict_label, char Status, int currentPage, int pageSize)
        {
            //只查询没有被删除的数据
            IQueryable<Sys_Dict_Data> query = myDbContext.Sys_Dict_Data.Where(d => d.Del_flag == '0');
            //条件筛选
            if (!string.IsNullOrEmpty(Dict_type))
            {
                query = query.Where(d => d.Dict_type == Dict_type);
            }
            if (!string.IsNullOrEmpty(Dict_label))
            {
                query = query.Where(d => d.Dict_label == Dict_label);
            }
            if (Status == '0' || Status == '1')
            {
                query = query.Where(d => d.Status == Status);
            }
            //分页查询
            var data = query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
            data = data.OrderBy(d => d.Dict_sort).ToList();
            return data;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Postsys_Dict_DatasAdd(Sys_Dict_Data data)
        {
            await myDbContext.Sys_Dict_Data.AddAsync(data);
            return await myDbContext.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Putsys_Dict_DatasDelete(Sys_Dict_Data data)
        {
            Sys_Dict_Data Dictdata = await myDbContext.Sys_Dict_Data.FindAsync(data.Dict_code);
            Dictdata.Del_flag = data.Del_flag;
            return await myDbContext.SaveChangesAsync();

        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Putsys_Dict_DatasUpdate(Sys_Dict_Data data)
        {
            Sys_Dict_Data Dictdata = myDbContext.Sys_Dict_Data.Find(data.Dict_code);
            Dictdata.Dict_label = data.Dict_label;
            Dictdata.Dict_value = data.Dict_value;
            Dictdata.Dict_sort = data.Dict_sort;
            Dictdata.Is_default = data.Is_default;
            Dictdata.Status = data.Status;
            Dictdata.Update_by = data.Update_by;
            Dictdata.Update_time = data.Update_time;
            Dictdata.Remark = data.Remark;
            myDbContext.Sys_Dict_Data.Update(Dictdata);
            return myDbContext.SaveChangesAsync();
        }
    }
}
